/**
 * @Author: yeminglong
 * @Date: 2024-09-19 18:46:15
 * @LastEditors: yeminglong
 * @LastEditTime: 2024-09-20 10:28:30
 * @Description:
 */
import { getQueryVariable, setToken } from '@/token'
import LoginServiceApi from '@/views/auth/login/assets/loginServiceApi'
import { flattenTree } from '@/utils'
import router from '@/router'
import store from '@/store'

async function digitalBaseSso(code = '') {
  try {
    const { data } = await LoginServiceApi.digitalBaseSso(code)
    setToken(data?.access_token)

    const [userInfo, routers] = await store.dispatch('loginStore/fetchUserInfo')

    const defaultUrl = flattenTree(routers).filter(v => v.query && v.query === 'menu')[0]?.path
    if (defaultUrl) {
      location.href = `index.html/#${defaultUrl}`
    } else {
      location.href = 'index.html/#/welcome'
    }
  } catch (e) {
    location.href = 'index.html/#/login'
  }
}
const code = getQueryVariable('code') || ''

if (code) {
  digitalBaseSso(code)
}
